<template>
  <div>
    <div>
      <el-form :inline="true" :model="car1" class="demo-form-inline">
        <el-form-item label="车辆类型:">
          <el-select v-model="car1.cartypeId" placeholder="请选择">
            <el-option
              v-for="item in carTypes"
              :key="item.cartypeId"
              :label="item.cartypeName"
              :value="item.cartypeId">
            </el-option>
          </el-select>
          <el-input v-model="car1.carPlate" placeholder="车牌号码" style="width: 180px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="findCar" icon="el-icon-search">搜索</el-button>
          <el-button type="info" @click="cleanSelect" icon="el-icon-refresh">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <hr>
    <div>
      <el-button type="primary" @click="dialogVisible = true" icon="el-icon-circle-plus">新增车辆</el-button>
    </div>
    <br>

    <template>
      <el-table
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="carId"
          label="序号"
          width="80"
          align="center">
        </el-table-column>
        <el-table-column
          prop="carNum"
          label="车辆编号"
          width="180"
          align="center">
        </el-table-column>
        <el-table-column
          prop="carTypes.cartypeName"
          label="车辆类型"
          width="180"
          align="center">
        </el-table-column>
        <el-table-column
          prop="carPlate"
          label="车牌号码"
          width="180"
          align="center">
        </el-table-column>
        <el-table-column
          prop="carrier.carrierName"
          label="所属承运商"
          width="180"
          align="center">
        </el-table-column>
        <el-table-column
          prop="maxWeight"
          label="准载重量（千克）"
          width="140"
          align="center">
        </el-table-column>
        <el-table-column
          prop="maxVolume"
          label="准载体积（方）"
          width="130"
          align="center">
        </el-table-column>
        <el-table-column
          prop="status"
          label="车辆状态"
          width="100">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === 0 ? 'danger':'success'"
              disable-transitions>
              <span v-if="scope.row.status==0">已发车</span>
              <span v-else>在库</span>
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-s-tools"
              @click="dialogVisible1 = true,handleEdit(scope.$index, scope.row)">查看详情
            </el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-remove"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2,4,6]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!--添加弹出框-->
    <el-dialog
      title="新增车辆"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
       @close="handCloseListener">

      <el-form ref="car" :model="newcar" label-width="80px">
        <el-form-item label="车辆类型">
          <el-select v-model="newcar.cartypeId" placeholder="请选择">
            <el-option
              v-for="item in carTypes"
              :key="item.cartypeId"
              :label="item.cartypeName"
              :value="item.cartypeId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车牌号码">
          <el-input v-model="newcar.carPlate" placeholder="请输入车牌号码" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="承运商">
            <el-select v-model="newcar.carrierId" placeholder="请选择所属承运商">
              <el-option
                v-for="item in carriers"
                :key="item.carrierId"
                :label="item.carrierName"
                :value="item.carrierId">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="准载重量" >
          <el-input  v-model="newcar.maxWeight" placeholder="请输入准载重量" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="准载体积" >
          <el-input  v-model="newcar.maxVolume" placeholder="请输入准载体积" style="width: 220px"></el-input>
        </el-form-item>

      </el-form>
      <el-button type="primary" @click="dialogVisible = false,onSubmit()">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>

    </el-dialog>
    <!--编辑车辆-->
    <el-dialog
      title="修改车辆信息"
      :visible.sync="dialogVisible1"
      width="30%"
      :before-close="handleClose1">

      <el-form ref="car" :model="car" label-width="80px">
        <el-form-item label="车辆编号">
          <el-input v-model="car.carNum" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="车辆类型">
          <el-select v-model="car.cartypeId" placeholder="请选择" style="width: 220px">
            <el-option
              v-for="item in carTypes"
              :key="item.cartypeId"
              :label="item.cartypeName"
              :value="item.cartypeId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车牌号码">
          <el-input v-model="car.carPlate" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="承运商">
          <el-select v-model="car.carrierId" placeholder="请选择所属承运商" style="width: 220px">
            <el-option
              v-for="item in carriers"
              :key="item.carrierId"
              :label="item.carrierName"
              :value="item.carrierId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="准载重量" >
          <el-input v-model="car.maxWeight" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="准载体积" >
          <el-input v-model="car.maxVolume" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="工作状态" >
          <el-select v-model="car.status"   placeholder="请选择" style="width: 220px" >

            <el-option
              v-for="status in status1"
              :key="status.value"
              :label="status.label"
              :value="status.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-button @click="dialogVisible1 = false">取 消</el-button>
      <el-button type="primary" @click="dialogVisible1 = false,onSubmit1()">确 定</el-button>

    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "type",
    data() {
  return {
    status1:[{
      value:1,
      label:'在库'
    },{
      value: 0,
      label: '已发车'
    }],
    carTypes:[],
    carriers:[],
    tableData: [],
    currentPage: 1,
    pageSizes: [2, 4, 6],
    pageSize: 2,
    total: 0,
    formInline: {
      cartype: '',
      cartypeId:'',
      carPlate:''

    },
    multipleSelection: [],
    dialogVisible: false,
    dialogVisible1: false,
    newcar:{
      carId: '',
      carNum: '',
      carPlate: '',
      carType: '',
      carrierName:'',
      maxWeight:'',
      maxVolume:'',
      carrierId:'',
      cartypeId:'',
      status:'1'
    },
    car: {
      carId: '',
      carNum: '',
      carPlate: '',
      carType: '',
      carrierName:'',
      maxWeight:'',
      maxVolume:'',
      carrierId:'',
      cartypeId:'',
      status:''

    },
    car1:{
      cartypeId:"",
      carPlate:""
    },
    carPlate:'',
  }
},
methods: {
  handCloseListener(){
    this.newcar={}
  },

    handleSelectChange(){},
    // 状态栏
  filterTag(value, row) {
    return row.tag === value;
  },
  findCar(currentPage,pageSize) {
    if(this.carPlate || this.cartypeId){
      this.currentPage=1;
    }
    this.$http({
      method: "GET",
      url: "http://localhost:8090/car",
      params: {carPlate:this.car1.carPlate,
        cartypeId:this.car1.cartypeId,
        currentPage: this.currentPage,
        pageSize: this.pageSize}
    }).then(resp => {
      console.log(resp)
      this.tableData = resp.data.data.records;
      this.total = resp.data.data.total;
    })
  },
  onSubmit() {
    this.$http({

        method: "POST",
        url: "http://localhost:8090/car",
        data: this.newcar
      }).then(resp => {
        this.newcar={carId: '',
          carNum: '',
          carPlate: '',
          maxVolume: '',
          maxWeight:'',
          createTime:'',
          carrierId:'',
          cartypeId:'',
          status: '1'
        };
      this.$message({
        type: 'success',
        message: '新增成功!'
      })
        this.findCar(1, 2);
      })
    },
    onSubmit1() {

      this.$http({
        method: "PUT",
        url: "http://localhost:8090/car",
        data: this.car
      }).then(resp => {
        this.car={
          carId: '',
          carNum: '',
          carPlate: '',
          cartypeId: '',
          maxWeight:'',
          maxVolume:'',
            status:''
        };
        if (resp.data.code === 100000) {
          this.$message({
            message:'修改'+ resp.data.msg,
            type: "success",
          });
        } else {
          this.$message.error('修改'+resp.data.msg+'，请重试');
        }
        this.findCar(this.currentPage, this.pageSize);
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    handleClose1(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    findById(id) {
      this.$http({
        type: "GET",
        url: `http://localhost:8090/car/${id}`
      }).then(resp => {
        console.log(resp);
        console.log(resp.data);
        this.car = resp.data.data
      })


    },
    //编辑的方法
    handleEdit(index, row) {
      let id = row.carId;
      this.findById(id);
    },
    //删除的方法
    handleDelete(index, row) {
      let id = row.carId;
      let a = this.currentPage;
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          method: "DELETE",
          url: `http://localhost:8090/car/${id}`

        }).then(resp => {
          console.log(resp);
          console.log(resp.data);
          if (a > (this.total / this.pageSize)) {
            this.findCar(a - 1, this.pageSize);
          } else {
            this.findCar(a, this.pageSize);
          }

        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });


    },
    // 页面大小的触发函数
    handleSizeChange(val) {

      //拿到当前的页面大小
      this.pageSize = val;
      //重新加载 数据
      this.findCar(this.currentPage, this.pageSize);
    },
    //当前页面变化触发的函数
    handleCurrentChange(val) {

      this.currentPage = val;
      // 当前第几页发生了变化  重新加载数据
      this.findCar(this.currentPage, this.pageSize);
    },
    // findall(currentPage,pageSize) {
    //   this.$http({
    //     method: "GET",
    //     url: "http://localhost:8090/car",
    //     params: {
    //       currentPage: currentPage,
    //       pageSize: pageSize
    //     }
    //   }).then(resp => {
    //     this.tableData = resp.data.data.records
    //     this.total = resp.data.data.total;
    //   })
    //
    // },
    getCarType(){
      this.$http({
        method:"GET",
        url:"http://localhost:8090/carType"
      }).then(resp=>{
        console.log(resp.data);
        this.carTypes = resp.data.data;
        }
      )
    },
  getCarrier(){
    this.$http({
      method:"GET",
      url:"http://localhost:8090/carrier"
    }).then(resp=>{
        console.log(resp.data);
        this.carriers = resp.data.data;
      }
    )
  },
  cleanSelect(){
      this.car1.carPlate = '';
      this.car1.cartypeId='';
    this.findCar(1, 2);
  }
  },
  created() {
    this.findCar(1, 2);
    this.getCarType();
    this.getCarrier()
  }
}
</script>

<style>
</style>
